package edu.ccut.saturn.basevalidate.businessvalidate;

import edu.ccut.saturn.component.DefaultSaturnValidate;
import edu.ccut.saturn.component.ValidateResult;

/**
 * 只有数值可以进行区间校验
 * 
 * @author tony
 * 
 */
public class RangeValidate extends DefaultSaturnValidate {

//	private static final String doubleRegExpr = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0|-?[1-9]\\d*)$";

	private static final String accountRegExpr = "^-?(\\d*)([.]{0,1})(\\d{1,})$";
	
	public void doValidate(Object value, Object[] args, ValidateResult result) {
		
		double origin = 0;
		double min = 0;
		double max = 0;
		
		if (args == null || args[0] == null || args[1] == null) {
			sendError(_ARGERROR_, value, result);
			return;
		}
		if (!(value.toString()).matches(accountRegExpr)) {
			sendError(_VALUEERROR_, value, result);
			return;
		}	
		
		// 数值统一按双精度比较	
		origin = Double.parseDouble(value.toString());		
		min = Double.parseDouble((String) args[0]);
		max = Double.parseDouble((String) args[1]);
	
		if (min > origin || max < origin) {
			sendError(_ERROR_, value, result);
		}
	}
}
